<?php
session_start();
Header("Pragma: No-cache");
set_include_path('./../../nearestCaches/library' . PATH_SEPARATOR . get_include_path());
$PEAR_PATH = 'PEAR.php';
include ('Request/Request.php');
include "functions.php";
$lang = getLang();
$versionInt = getClientIntVersion();

    global $viewstate;
    global $postback;
    global $cookie;
    
    if ($_GET['show'] == 'search') {
    	$keyword = $_POST["keyword"];
    	$url = "http://www.geocaching.com/seek/nearest.aspx?key=".urlencode($keyword)."&submit4=Go";
    	$req = & new HTTP_Request($url);
    	$req->setMethod(HTTP_REQUEST_METHOD_GET);
    	$req->addHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
    	$req->addCookie('ASP.NET_SessionId', $_SESSION['cookie']);
    	$req->sendRequest();
    	$response = $req->getResponseBody();
    	$viewstate = parseBetween('<table id="ctl00_ContentBody_dlResults" class="SearchResultsTable" cellspacing="0" border="0" style="border-collapse:collapse;">', '</table>');
	$viewstate = trim(str_replace('"', '', $viewstate));
    	//parse table with nearest caches
    	$caches = parseTable($viewstate);
    	//get cache name, distance and link
    	$parsedCaches = array();
    foreach($caches as $cache) {
        if(!strstr($cache[5], "<strike>")) {
            $position = 0;
            $response = $cache[2];
            $typeNumber = parseBetween('WptTypes/', '.gif');
            switch($typeNumber) {
                case '2': $type = 'TR'; break;
                    case '3': $type = 'MU'; break;
                        case '4': $type = 'VI'; break;
                            case '5': $type = 'LB'; break;
                                case '6': $type = 'EV'; break;
                                    case '8': $type = 'UN'; break;
                                        case '11': $type = 'WC'; break;
                                            case '12': $type = 'LL'; break;
                                                case '13': $type = 'CI'; break;
                                                    case 'earthcache': $type = 'EC'; break;
                                                        case '1304': $type = 'AME'; break;
                                                            case '1858': $type = 'WH'; break;
                                                                case 'mega': $type = 'ME'; break;
                                                                    case 'ape_32': $type = 'APE'; break;
                                                                        default: $type = '';
                                                                        }
                                                                        $a = strpos($cache[0], 'compass/')+8;
                                                                        $b = strpos($cache[0], '.gif');
                                                                        $compas = removeHTML(trim(substr($cache[0], $a, $b-$a)), false);
                                                                        if (strLen($compas) > 2) {
                                                                        	$compas = '';
                                                                        } else {
                                                                        	$compas = $lang[$compas];
                                                                        	$compas .= ' - ';
                                                                        }
                                                                        $a = strpos($cache[0], '<br />')+6;
                                                                        $b = strlen($cache[0]);
                                                                        $distance = removeHTML(trim(substr($cache[0], ($a-$b))), false);
                                                                        $response = $cache[5];
                                                                        $link = parseBetween('<a href=', '>');
                                                                        $response = $cache[5];
                                                                        $name = removeHTML(parseBetween('>', '</a>'), false);
                                                                        $link = explode('?', $link);
                                                                        $params = explode('&', $link[1]);
                                                                        $params_arr = array();
                                                                        foreach($params as $param) {
                                                                            $foo = explode('=', $param);
                                                                            $params_arr[$foo[0]] = $foo[1];
                                                                        }
                                                                        if($versionInt && $versionInt < 91) {
                                                                            $parsedCaches[] = '<li><a href="'.SERVICE_URL.'cache.php?guid='.$params_arr['guid'].'" class="listItem">['.$type.'] '.$distance.' '.$compas.$name.'</a></li>'."\n";
                                                                        }
                                                                        else
                                                                        {
                                                                            $parsedCaches[] = '<li><img src="'.SERVICE_URL.'icons/'.$type.'.png" /><a href="'.SERVICE_URL.'cache.php?guid='.$params_arr['guid'].'" class="listItem">'.$distance.' '.$compas.$name.'</a></li>'."\n";
                                                                        }
                                                                    }
                                                                }
                                                                if(count($parsedCaches)==0) {	//output if no caches found
                                                                    $html = '<?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html>
              <head>
                <title>Warning</title>
              </head>
              <body class="alert">
            <p class="text">'.$lang['keyword_not_found'].'.</p>
        <locify:timeout value="6000" />';
                                                                } else {	//output list of caches
                                                                    $html = '<?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html>
              <head>
                <title>'.$lang['keyword_search'].'</title>
              </head>
              <body class="list"><ul>'."\n";
                                                                    foreach($parsedCaches as $parsedCache) $html .= $parsedCache;
                                                                    $html .= "</ul>\n";
                                                                }
                                                                $html .= "</body>\n</html>";

                                                                echo $html;

    } else {
	echo '<?xml version="1.0" encoding="utf-8" ?>
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml" xmlns:locify="http://client.locify.com/locify_ns/1.0" xml:lang="en" lang="en">
		<head>
    		<title>'.$lang['keyword_search'].'</title>
  		</head>
  		<body class="form">
    		<form action="'.SERVICE_URL.'keyword.php?show=search" method="post">
		<fieldset>
			<b>'.$lang['keyword'].':</b> <br />
			<label>&nbsp;<input type="text" name="keyword" value="" /></label><br />
         		<input type="submit" value="'.$lang['search'].'" /><br />
		</fieldset>
    		</form>
  		</body>
		</html>';

}
?>
